package ru.belokuriha_online.resort_hotels_belokuriha;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Environment;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.*;
import android.content.*;
import android.content.DialogInterface.OnClickListener;
import android.widget.ListView;


public class Seachprice extends Activity implements TextWatcher {

	
	String filepricesPath1;	
	String fileaboutPath;
	String namesan;
	String id_namesan;
	String namesanp;
	String id_namesanp;
	String spprisepath;
	String spprisepathfull;

	ArrayList<HashMap<String, String>> Datams;

	HashMap<String, String> msan;
	ListView listprice_seach;
	SimpleAdapter seachAdapter;
	public ArrayList<String> listpodskaz;
	public ArrayAdapter<String> listpodskazadapter;
	AutoCompleteTextView Autopodskaz;
	Integer seeseach, norezult;
	String strseach;
	String p_0, p_0_0, p_1, p_2, p_3, p_4, p_5, p_6, p_7;
	String sp_0, sp_0_0, sp_1, sp_2, sp_3, sp_4, sp_5, sp_6, sp_7;

	private CharSequence[] spisokcontmenu;
	AlertDialog.Builder dialogcontmenu;

	TextView noseach;
	int FlagOnlain;
	HttpURLConnection urlConnectiond1;
	XmlPullParser parserp;
	InputStream xmlfilep;
	HttpURLConnection urlConnectiond;
	XmlPullParser parser;
	InputStream xmlfile;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.seachprise);
		try {
		spisokcontmenu = getResources().getTextArray(R.array.contmenus);
		spprisepath = getIntent().getExtras().getString("spprisepath");
		spprisepathfull = getIntent().getExtras().getString("spprisepathfull");
		FlagOnlain = getIntent().getExtras().getInt("FlagOnlain");
		listprice_seach = (ListView) findViewById(R.id.listseachs);
		noseach = (TextView) findViewById(R.id.norezult);
		//***\\
		listpodskaz = new ArrayList<String>(); 
		try {
			
			XmlPullParserFactory parserpulp = XmlPullParserFactory.newInstance();
			parserpulp.setValidating(false);
			parserp = parserpulp.newPullParser();
			File xmlfilepricesp = new File(spprisepathfull);
			xmlfilep = new FileInputStream(xmlfilepricesp);
			parserp.setInput(xmlfilep, null);
			
			namesanp = "";
			id_namesanp = "";

			Autopodskaz = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextViewSeach); 

			while (parserp.getEventType()!= XmlPullParser.END_DOCUMENT) {

				if (parserp.getEventType() == XmlPullParser.START_TAG
						&& parserp.getName().equals("sanatori") 
						&& parserp.getDepth() == 2) {
					namesanp = parserp.getAttributeValue(0);
					id_namesanp = parserp.getAttributeValue(1);
					if (namesanp == null) {
						namesanp = "";	
					} 
					if (id_namesanp == null) {
						id_namesanp = "";	
					} 

				}
				if (parserp.getEventType() == XmlPullParser.START_TAG
						&& parserp.getName().equals(id_namesanp) 
						&& parserp.getDepth() == 3) {

					String pods_0 = namesanp;

					if (pods_0 != null) {
						if (listpodskaz.contains(pods_0) == false) {
							listpodskaz.add(pods_0);
						}
					}

					String pods_1 = parserp.getAttributeValue(0);
					if (pods_1 != null) {
						if (listpodskaz.contains(pods_1) == false) {
							listpodskaz.add(pods_1);
						}

					}
					String pods_2 = parserp.getAttributeValue(1).toString();
					if (pods_2 != null) {
						if (listpodskaz.contains(pods_2) == false) {
							listpodskaz.add(pods_2);
						}
					}
					String pods_3 = parserp.getAttributeValue(2).toString();
					if (pods_3 != null) {
						if (listpodskaz.contains(pods_3) == false) {
							listpodskaz.add(pods_3);	
						}
					}
					String pods_4 = parserp.getAttributeValue(3).toString();
					if (pods_4 != null) {
						if (listpodskaz.contains(pods_4) == false) {
							listpodskaz.add(pods_4);	
						}
					}
					String pods_5 = parserp.getAttributeValue(4).toString();
					if (pods_5 != null) {
						if (listpodskaz.contains(pods_5) == false) {
							listpodskaz.add(pods_5);	
						}
					}
					String pods_6 = parserp.getAttributeValue(5).toString();
					if (pods_6 != null) {
						if (listpodskaz.contains(pods_6) == false) {
							listpodskaz.add(pods_6);	
						}
					}



				}

				parserp.next();
			}
			xmlfilep.close();			

			Autopodskaz.addTextChangedListener(this);
			listpodskazadapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, listpodskaz);
			Autopodskaz.setAdapter(listpodskazadapter);

		}
		catch (Throwable t) {

		}

		//***\\		





		final TextView exitseach =(TextView) findViewById(R.id.textViewCloseseach);
		exitseach.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();	
			}	
		});

		final TextView clearseach =(TextView) findViewById(R.id.textViewClearseach);
		clearseach.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Autopodskaz.setText("");
				listprice_seach.setAdapter(null);
				noseach.setVisibility(View.GONE);
			}	
		});

		final Button seachbutton =(Button) findViewById(R.id.buttonSeach);
		seachbutton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				seachprices();

			}	
		});


		Autopodskaz.setOnKeyListener(new View.OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(event.getAction() == KeyEvent.ACTION_DOWN && 
						(keyCode == KeyEvent.KEYCODE_ENTER)) 
				{ 		            	  				
					seachprices(); 
					return true; 
				} 
				return false; 
			} 
		} 
				);

	} catch (Exception e) {
		// TODO: handle exception
	}

	}

	public void seachprices() {


		////*\\\\

		Autopodskaz = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextViewSeach);
		noseach = (TextView) findViewById(R.id.norezult);
		noseach.setVisibility(View.GONE);
		if (Autopodskaz.getText().toString().equals("")) {
			//
			Toast.makeText(getApplicationContext(),getString(R.string.no_seach), Toast.LENGTH_LONG).show();
		} else {
			strseach = Autopodskaz.getText().toString();
			if (strseach == null) {
				strseach = "";
			} else {
				strseach = Autopodskaz.getText().toString().toLowerCase();
			}

			Datams =  new ArrayList<HashMap<String, String>>();
			try {
				
				XmlPullParserFactory parserpull = XmlPullParserFactory.newInstance();
				parserpull.setValidating(false);
				XmlPullParser parser = parserpull.newPullParser();
				File xmlfileprices1 = new File(spprisepathfull);
				xmlfile = new FileInputStream(xmlfileprices1);
				parser.setInput(xmlfile, null);
				
				
				namesan = "";
				id_namesan = "";
				seeseach = 0;
				norezult = 0;
				while (parser.getEventType()!= XmlPullParser.END_DOCUMENT) {

					if (parser.getEventType() == XmlPullParser.START_TAG
							&& parser.getName().equals("sanatori") 
							&& parser.getDepth() == 2) {
						namesan = parser.getAttributeValue(0);
						id_namesan = parser.getAttributeValue(1);
						if (namesan == null) {
							namesan = "";	
						}
						if (id_namesan == null) {
							id_namesan = "";	
						}						

					}
					if (parser.getEventType() == XmlPullParser.START_TAG
							&& parser.getName().equals(id_namesan) 
							&& parser.getDepth() == 3) {



						msan = new HashMap<String, String>();

						//-------
						sp_0 = namesan;
						sp_0_0 = id_namesan;
						if (sp_0 == null) {
							sp_0 = "";

						}  else {

							sp_0 = namesan.toLowerCase();
						}
						if (sp_0.indexOf(strseach) != -1) {
							seeseach = 1;
						}
						//-------

						sp_1 = parser.getAttributeValue(0);
						if (sp_1 == null) {

							sp_1 = "";
						}  else {

							sp_1 = parser.getAttributeValue(0).toLowerCase();
						}
						if (sp_1.indexOf(strseach) != -1) {
							seeseach = 1;
						}
						//-------

						sp_2 = parser.getAttributeValue(1);
						if (sp_2 == null) {

							sp_2 = "";
						}  else {

							sp_2 = parser.getAttributeValue(1).toLowerCase();
						}
						if (sp_2.indexOf(strseach) != -1) {
							seeseach = 1;
						}
						//-------

						sp_3 = parser.getAttributeValue(2);
						if (sp_3 == null) {

							sp_3 = "";
						}  else {

							sp_3 = parser.getAttributeValue(2).toLowerCase();
						}
						if (sp_3.indexOf(strseach) != -1) {
							seeseach = 1;
						}
						//-------

						sp_4 = parser.getAttributeValue(3);
						if (sp_4 == null) {

							sp_4 = "";
						}  else {

							sp_4 = parser.getAttributeValue(3).toLowerCase();
						}
						if (sp_4.indexOf(strseach) != -1) {
							seeseach = 1;
						}
						//-------

						sp_5 = parser.getAttributeValue(4);
						if (sp_5 == null) {

							sp_5 = "";
						}  else {

							sp_5 = parser.getAttributeValue(4).toLowerCase();
						}
						if (sp_5.indexOf(strseach) != -1) {
							seeseach = 1;
						}
						//-------

						sp_6 = parser.getAttributeValue(5);
						if (sp_6 == null) {

							sp_6 = "";
						}  else {

							sp_6 = parser.getAttributeValue(5).toLowerCase();
						}
						if (sp_6.indexOf(strseach) != -1) {
							seeseach = 1;
						}
						//-------




						if (seeseach == 1) {

							msan.put("p_0", namesan);
							msan.put("p_0_0", id_namesan);
							msan.put("p_1", parser.getAttributeValue(0));
							msan.put("p_2", parser.getAttributeValue(1));                    
							msan.put("p_3", parser.getAttributeValue(2));
							msan.put("p_4", parser.getAttributeValue(3));
							msan.put("p_5", parser.getAttributeValue(4));
							msan.put("p_6", parser.getAttributeValue(5));
							msan.put("p_7", parser.nextText());

							Datams.add(msan);
							norezult = 1;

						}
						seeseach = 0;
					}


					parser.next();
				}
				xmlfile.close();

				String[] from = {"p_0","p_1","p_2","p_3","p_4","p_5","p_6","p_7"};
				int[] to = {R.id.seachtextView_san,R.id.seachtextView_category,R.id.seachtextView_area_n,R.id.seachtextView_razm,R.id.seachtextView_time,R.id.seachtextView_food,R.id.seachtextView_price,R.id.seachtextView_prim}; 

				//  
				seachAdapter = new SimpleAdapter(this, Datams, R.layout.seachlist, from, to);


				listprice_seach.setAdapter(seachAdapter);

				listprice_seach.setOnItemClickListener(new OnItemClickListener() { 
					@Override
					public void onItemClick(AdapterView<?> arg0, View v,
							int position, long id) {
						Dialmenuseach(position);

					} 
				});


				if (norezult == 0) {	
					noseach.setVisibility(View.VISIBLE);					
				}
				else {
					noseach.setVisibility(View.GONE);
				}
				
			}
			catch (Throwable t) {

			}

		}

		////*\\\\

	}
	private void Dialmenuseach(final int position) {
		dialogcontmenu = new AlertDialog.Builder(this);
		dialogcontmenu.setTitle("");		
		dialogcontmenu.setItems(spisokcontmenu, 
				new DialogInterface.OnClickListener() {					
			@Override
			public void onClick(DialogInterface dialog, int item) {
				switch (item) { 

				case 0:
					try{

						Intent intent = new Intent(getApplicationContext(),Mainprice.class);

						String sanatori_name = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_0").toString();
						String idd_name = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_0_0").toString();
						String mpp_1 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_1").toString();
						String mpp_2 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_2").toString();
						String mpp_3 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_3").toString();
						String mpp_4 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_4").toString();
						String mpp_5 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_5").toString();
						String mpp_6 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_6").toString();
						Integer flags_srav = 1;
						intent.putExtra("flags_srav", flags_srav);
						intent.putExtra("san_name", sanatori_name);
						intent.putExtra("id_name", idd_name);				
						intent.putExtra("spprisepath", spprisepath);
						intent.putExtra("spprisepathfull", spprisepathfull);
						intent.putExtra("FlagOnlain", FlagOnlain);
						intent.putExtra("mpp_1", mpp_1);
						intent.putExtra("mpp_2", mpp_2);
						intent.putExtra("mpp_3", mpp_3);
						intent.putExtra("mpp_4", mpp_4);
						intent.putExtra("mpp_5", mpp_5);
						intent.putExtra("mpp_6", mpp_6);
						startActivityForResult(intent,0);

					} catch (Exception e) {

					}


					break; 


				case 1:

					try {
						Intent intentsea = new Intent(getApplicationContext(), Sravneniye.class);
						String sanatori_name = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_0").toString();
						String idd_name = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_0_0").toString();
						String mmpp_1 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_1").toString();
						String mmpp_2 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_2").toString();
						String mmpp_3 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_3").toString();
						String mmpp_4 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_4").toString();
						String mmpp_5 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_5").toString();
						String mmpp_6 = ((HashMap<String, String>)(seachAdapter.getItem(position))).get("p_6").toString();
						Integer flags_srav = 1;
						intentsea.putExtra("flags_srav", flags_srav);
						intentsea.putExtra("san_name", sanatori_name);
						intentsea.putExtra("id_name", idd_name);
						intentsea.putExtra("spprisepath", spprisepath);
						intentsea.putExtra("spprisepathfull", spprisepathfull);
						intentsea.putExtra("FlagOnlain", FlagOnlain);
						intentsea.putExtra("mmpp_1", mmpp_1);
						intentsea.putExtra("mmpp_2", mmpp_2);
						intentsea.putExtra("mmpp_3", mmpp_3);
						intentsea.putExtra("mmpp_4", mmpp_4);
						intentsea.putExtra("mmpp_5", mmpp_5);
						intentsea.putExtra("mmpp_6", mmpp_6);
						startActivityForResult(intentsea,3);




					} catch (Exception e) {

					}
					break; 

				}       					
			}
		}				
				).create().show();


	}


	public boolean onCreateOptionsMenu(Menu menu) { 

		getMenuInflater().inflate(R.menu.pmenu, menu);

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) { 

		if (FlagOnlain == 0) {
			menu.findItem(R.id.loaditem1).setVisible(true);
			menu.findItem(R.id.loaditem1).setEnabled(true);
			} else
			{			
			menu.findItem(R.id.loaditem1).setEnabled(false);
			menu.findItem(R.id.loaditem1).setVisible(false);
			}
			menu.findItem(R.id.pitem2).setEnabled(true);
			menu.findItem(R.id.abitem3).setEnabled(true);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) { 

		case R.id.loaditem1: 
			GoMain();
			break; 


		case R.id.pitem2:

			try {
				Intent Preferintent = new Intent(getApplicationContext(),PreferActivity.class);
				Preferintent.putExtra("FlagOnlain", FlagOnlain);
				startActivityForResult(Preferintent, 0);
			} catch (Exception e) {

			}
			break; 
		case R.id.abitem3: 

			try {
				Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);
				intentapp.putExtra("FlagOnlain", FlagOnlain);
				startActivityForResult(intentapp, 0);
			} catch (Exception e) {

			}
			break;
		}   

		return super.onOptionsItemSelected(item); 

	}
	private void GoMain() {
		AlertDialog.Builder buildergomain = new AlertDialog.Builder(this);
		buildergomain.setTitle(R.string.lt_app);		
		buildergomain.setMessage(R.string.lmmm_app);		
		buildergomain.setNegativeButton(R.string.qn_app, null);
		buildergomain.setPositiveButton(R.string.qy_app, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				setResult(7);
				finish(); 	            	

			}
		}).create().show();	
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == 7) {
			setResult(7);
			finish();
		}
		//
		if (resultCode ==  5) {
			try {
				openOptionsMenu();
			} catch (Exception e) {

			}
		}
		if (resultCode ==  9) {
			try {
				Intent Preferintent = new Intent(getApplicationContext(),PreferActivity.class);
				Preferintent.putExtra("FlagOnlain", FlagOnlain);
				startActivityForResult(Preferintent,0);
			} catch (Exception e) {

			}
		}
		if (resultCode ==  10) {
			try{
				Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);
				intentapp.putExtra("FlagOnlain", FlagOnlain);
				startActivityForResult(intentapp,0);	
			} catch (Exception e) {

			}
		}

	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}


}
